package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.exception.BusException;
import cn.stylefeng.guns.sys.core.exception.enums.BusExceptionEnum;
import cn.stylefeng.guns.sys.core.properties.GzhConfig;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.db4.entity.Team;
import cn.stylefeng.guns.sys.modular.system.db4.mapper.TeamMapper;
import cn.stylefeng.guns.sys.modular.system.entity.*;
import cn.stylefeng.guns.sys.modular.system.mapper.*;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyGufenzhuanrangParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyYhkbgParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ProjectParam;
import cn.stylefeng.guns.sys.modular.system.model.result.ProjectResult;
import cn.stylefeng.guns.sys.modular.system.service.*;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class GcWxServiceImpl extends ServiceImpl<GcgMsgMapper, GcgMsg> implements GcWxService {

    private static Logger logger = LoggerFactory.getLogger(GcWxServiceImpl.class);

    @Resource
    private BtyWxMapper btyWxMapper;
    @Resource
    private TaskMapper taskMapper;
    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private BtyTxlService btyTxlService;
    @Resource
    private WeChatSendTempMsgService weChatSendTempMsgService;
    @Resource
    private BtyTxlMapper btyTxlMapper;
    @Resource
    private ProjectService projectService;
    @Resource
    private UserService userService;
    @Resource
    private TaskFlowMapper taskFlowMapper;
    @Resource
    private BtyRegionMapper regionMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private GzhConfig gzhConfig;
    @Resource
    private EmpMapper empMapper;
    @Resource
    private LoginService loginService;
    @Resource
    private DictService dictService;
    @Resource
    private TaskService taskService;
    @Resource
    private BtyGufenzhuanrangService btyGufenzhuanrangService;
    @Resource
    private TeamMapper teamMapper;
    @Resource
    private BtyWageService btyWageService;
    @Resource
    private BtyGufenzhuanrangMapper btyGufenzhuanrangMapper;
    @Resource
    private BtyYhkbgMapper btyYhkbgMapper;
    @Resource
    private BtyWageMapper btyWageMapper;

    // 跳转URL
    private String path = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=";
//    private String path = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx373650bf8de7f27d&redirect_uri=";
    private String type = "&response_type=code";
    private String scope = "&scope=snsapi_userinfo";
    private String state = "&state=STATE#wechat_redirect";


    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    /**
     * 本店未完成任务列表
     *
     * @param proId
     * @return
     */
    @Override
    public Map noFinish(int proId, Long userId) {
//    public List noFinish(int proId) {

        Map<String, Object> mapList = new HashMap<>();
        String roleIds = null;

        String phone = userMapper.getPhone(userId);
        if (phone != null && !"".equals(phone)) {
            //根据phone查询当前登陆者角色
            Map map = btyWxMapper.getRole(phone);
            if (map != null && map.size() != 0) {
                roleIds = map.get("roleId").toString();//获取当前登录者角色
            }
        }

        int status = 0;//0不是该店督导，1是督导
        //查询该店督导电话
        String ddTel = this.baseMapper.getDdTel(proId);

        //既是营运经理又是督导
        if (roleIds.contains("1308249608428101633") && roleIds.contains("1283292980305018882")) {
            //是该店督导
            if (ddTel.equals(phone)) {
                status = 1;
            } else {
                status = 0;
            }
            //是营运经理不是督导
        } else if (roleIds.contains("1308249608428101633") && !roleIds.contains("1283292980305018882")) {
            status = 0;
            //是督导不是营运经理
        } else if (roleIds.contains("1283292980305018882") && !roleIds.contains("1308249608428101633")) {
            status = 1;
        }

        List<Map> list = this.baseMapper.noFinish(proId);
        String proName = this.baseMapper.getProName(proId);
        List objects = new ArrayList<>();
        List list1 = new ArrayList<>();

        if (ToolUtil.isNotEmpty(list)) {
            for (Map map : list) {

                map.put("proId", proId);
                map.put("proName", proName);
                list1.add(map);

                if ((int) map.get("id") == 36) {
                    objects.add(36);
                }
                if ((int) map.get("id") == 37) {
                    objects.add(37);
                }
                if ((int) map.get("id") == 38) {
                    objects.add(38);
                }
            }
        }

        if (objects.size() > 0) {

            Map map = new HashMap<>();
            map.put("name", "网上建店");
            map.put("id", 36);
            map.put("proId", proId);
            map.put("proName", proName);
            list1.remove(map);
            list1.add(map);

            Map map1 = new HashMap<>();
            map1.put("name", "线上套餐");
            map1.put("id", 37);
            map1.put("proId", proId);
            map1.put("proName", proName);
            list1.remove(map1);
            Map map2 = new HashMap<>();
            map2.put("name", "排队机");
            map2.put("id", 38);
            map2.put("proId", proId);
            map2.put("proName", proName);
            list1.remove(map2);

        }

        mapList.put("list", list1);
        mapList.put("roleIds", roleIds);
        mapList.put("status", status);
        return mapList;
//        return list1;
    }

    /**
     * 本店已完成任务列表
     *
     * @param proId
     * @return
     */
    @Override
    public List finish(int proId) {

        List<Map> list = this.baseMapper.finish(proId);
        String proName = null;
        List objects = new ArrayList<>();
        for (Map map : list) {
            proName = (String) map.get("proName");
            if ((int) map.get("id") == 36) {
                objects.add(36);
            }
            if ((int) map.get("id") == 37) {
                objects.add(37);
            }
            if ((int) map.get("id") == 38) {
                objects.add(38);
            }
        }

        Map map = new HashMap<>();
        map.put("name", "网上建店");
        map.put("id", 36);
        map.put("proName", proName);
        map.put("proId", proId);

        Map map1 = new HashMap<>();
        map1.put("name", "线上套餐");
        map1.put("id", 37);
        map1.put("proName", proName);
        map1.put("proId", proId);

        Map map2 = new HashMap<>();
        map2.put("name", "排队机");
        map2.put("id", 38);
        map2.put("proName", proName);
        map2.put("proId", proId);

        if (objects.size() == 3) {

            list.remove(map);
            list.add(map);

            list.remove(map1);
            list.remove(map2);
        } else {

            list.remove(map);
            list.remove(map1);
            list.remove(map2);
        }

        return list;
    }

    /**
     * 全部店已完成任务列表
     *
     * @param wxOpenId
     * @return
     */
    @Override
    public ResponseData finishShop(String wxOpenId, Long userId, String proName) {

        List<Map> list = new ArrayList<>();

        //查询当前登录者是否记录有电话
        String phone = null;
        if (ToolUtil.isEmpty(userId)) {
            phone = btyWxMapper.selectPhone(wxOpenId);
        } else {
            phone = userMapper.getPhone(userId);
        }

        if (phone != null && !"".equals(phone)) {
            //根据phone查询当前登陆者角色
            Map map = btyWxMapper.getRole(phone);
            if (map != null && map.size() != 0) {
                String roleIds = map.get("roleId").toString();//获取当前登录者角色

                //营运总监
                if (roleIds.contains("1305422678196678657") || roleIds.equals("1")) {
                    //查看全部店已完成任务
                    list = this.baseMapper.yyzjFinish(proName);
                    return ResponseData.success(list);

                    //营运经理
                } else if (roleIds.contains("1308249608428101633")) {
                    //营运经理查看自己店面下所有已完成任务
                    list = this.baseMapper.yyjlFfinish(phone, proName);
                    return ResponseData.success(list);

                    //督导
                } else if (roleIds.contains("1283292980305018882")) {
                    //督导查看自己店面下所有已完成任务
                    list = this.baseMapper.ddFinish(phone, proName);
                    return ResponseData.success(list);

                    //系统负责人(收银系统负责人)
                } else if (roleIds.contains("1310462425628737538")) {
                    //收银系统已完成
                    list = this.baseMapper.sjFinish(proName);
                    return ResponseData.success(list);

                    //可乐机负责人
                } else if (roleIds.contains("1314488482857709570")) {
                    //可乐机已完成
                    list = this.baseMapper.kljFinish(proName);
                    return ResponseData.success(list);

                    //网上建店负责人
                } else if (roleIds.contains("1307580056085250050")) {
                    //网上建店已完成
                    list = this.baseMapper.wsjdFinish(proName);
                    return ResponseData.success(list);

                    //设计、收银档案负责人
                } else if (roleIds.contains("1352167937466048514") || roleIds.contains("1370241296749101057")) {
                    //收银档案已完成
                    list = this.baseMapper.sydaFinish(proName);
                    return ResponseData.success(list);

                }
            }
        }
        return null;
    }


    /**
     * 全部店未完成任务列表
     *
     * @param wxOpenId
     * @return
     */
    @Override
    public List noFinishShop(String wxOpenId, Long userId, String proName) {
//        Page pageContext = getPageContext();
//        IPage page = null;

        List list = new ArrayList<>();

        //查询当前登录者是否记录有电话
        String phone = null;
        if (ToolUtil.isEmpty(userId)) {
            phone = btyWxMapper.selectPhone(wxOpenId);
        } else {
            phone = userMapper.getPhone(userId);
        }

//        //查询当前登录者是否记录有电话
//        String phone = btyWxMapper.selectPhone(wxOpenId);

        if (phone != null && !"".equals(phone)) {
            //根据wxOpenId查询当前登陆者角色
            Map map = btyWxMapper.getRole(phone);
            if (map != null && map.size() != 0) {
                String roleIds = map.get("roleId").toString();//获取当前登录者角色

                //营运总监
                if (roleIds.contains("1305422678196678657") || roleIds.equals("1")) {
                    //查看全部店未完成任务
//                    page = this.baseMapper.yyzjNoFinish(pageContext);
                    list = this.baseMapper.yyzjNoFinish(proName);

                    //营运经理
                } else if (roleIds.contains("1308249608428101633")) {
                    //营运经理查看自己店面下所有未完成任务
//                    page = this.baseMapper.yyjlNoFinish(pageContext, phone);
                    list = this.baseMapper.yyjlNoFinish(phone, proName);

                    //督导
                } else if (roleIds.contains("1283292980305018882")) {
                    //督导查看自己店面下所有未完成任务
//                    page = this.baseMapper.ddNoFinish(pageContext, phone);
                    list = this.baseMapper.ddNoFinish(phone, proName);

                    //系统负责人（收银系统负责人）
                } else if (roleIds.contains("1310462425628737538")) {
                    //已指定督导，且收银系统未完成
//                    page = this.baseMapper.sjNoFinish(pageContext);
                    list = this.baseMapper.sjNoFinish(proName);

                    //可乐机负责人
                } else if (roleIds.contains("1314488482857709570")) {
                    //已填营业执照信息，但是可乐机未完成
//                    page = this.baseMapper.kljNoFinish(pageContext);
                    list = this.baseMapper.kljNoFinish(proName);

                    //网上建店负责人
                } else if (roleIds.contains("1307580056085250050")) {
                    //已指定督导，且网上建店未完成
//                    page = this.baseMapper.wsjdNoFinish(pageContext);
                    list = this.baseMapper.wsjdNoFinish(proName);

                    //收银档案负责人
                }else if (roleIds.contains("1370241296749101057")){
                    //收银账号已建完，收银档案未建完
                    list = this.baseMapper.sydaNoFinish(proName);
                }
            }
        }
        return list;
    }


    /**
     * 新增任务信息
     *
     * @param taskStatus
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addRent(List<TaskStatus> taskStatus) throws ParseException {

        logger.info("==========" + taskStatus);

        //修改状态,所有修改操作数据库新增条数必须都大于0才会提交事务
        StringBuilder result = new StringBuilder();
        // 数据库修改条数
        int count = 0;

        if (ToolUtil.isNotEmpty(taskStatus)) {

            //任务状态
            List<TaskStatus> taskStatuses = new ArrayList<>();
            //任务流水
            List<TaskFlow> taskFlows = new ArrayList<>();
            //消息管理
            List<GcgMsg> gcgMsgs = new ArrayList<>();

            for (TaskStatus status : taskStatus) {

                //填写内容为固定项
                if (status.getTaskId() != null) {

                    int msg = taskMapper.getRent(status.getProId(), status.getTaskId());

                    //之前没有填写过信息
                    if (msg == 0) {

                        if (status.getStatus() == null) {
                            status.setStatus(1);//提交信息
                        }

                        //不是可乐机新增，不是到货日期
                        if (status.getTaskId() != 19 && status.getTaskId() != 15) {
                            taskStatuses.add(status);
                            //是可乐机修改状态
                        } else if (status.getTaskId() == 19) {
                            taskMapper.updateStatus(status);
                        }

                        //流程明细表
                        TaskFlow taskFlow = new TaskFlow();
                        taskFlow.setProId(status.getProId());
                        taskFlow.setTaskId(status.getTaskId());

                        if (status.getTaskName() != null || status.getTaskName() != "") {
                            taskFlow.setTaskName(status.getTaskName());
                        }
                        taskFlow.setCreateUser(status.getCreateId());
                        taskFlow.setCreateTime(new Date());

                        //不是到货日期
                        if (status.getTaskId() != 15){
                            if (status.getStatus() != null) {
                                taskFlow.setStatus(status.getStatus());
                            } else {
                                taskFlow.setStatus(1);
                            }
                            //到货日期
                        }else {
                            taskFlow.setStatus(0);
                        }

                        taskFlows.add(taskFlow);

                        //查询店名
                        String dm = taskMapper.selectDm(status.getProId());
                        //任务名称
                        String taskName = taskMapper.getName(status.getTaskId());

//                        //查询该工程的城市,营运经理,开业时间
//                        Project project = projectMapper.selectProject(status.getProId());

                        //可乐机
                        if (status.getTaskId() == 19) {

                            GcgMsg gcgMsg = new GcgMsg();
                            gcgMsg.setBusinessId(status.getProId());//业务id
                            gcgMsg.setTaskId(status.getTaskId());//任务id
                            gcgMsg.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                            gcgMsg.setEndTime(new Date());//任务完成时间

                            gcgMsgs.add(gcgMsg);


                            //通知督导
                            String now = DateUtil.getNow();//当前时间
                            String data = dm + "可乐机已确认";
                            JSONObject tempObj = weChatSendTempMsgService.setTempParam(dm, null, "可乐机已确认", "可乐机已确认", now, data);

                            //推送督导可乐机已确认消息
                            Map<String, String> openidMap = projectService.selectDd(status.getProId());
                            //督导
                            String dd = openidMap.get("dd");
                            //督导id
                            User user = btyWxMapper.selectUser(dd);
                            String[] openid = new String[]{user.getOpenid()};

                            //可乐机url（督导）
                            String kljUrl = BusConst.KLJDDURL + "&proId=" + status.getProId() + "&taskId=" + status.getTaskId() + "&userId=" + user.getUserId();
                            String url = path + gzhConfig.getAppid() + "&redirect_uri=" + URLEncoder.encode(kljUrl) + type + scope + state;

                            //推送督导消息
                            weChatSendTempMsgService.sendmsg(openid, url, tempObj);
                            logger.info("======已推送督导可乐机已确认消息======");

                            //消息表中新增收可乐机消息
                            GcgMsg gcgMsg2 = new GcgMsg();
                            gcgMsg2.setBusinessId(status.getProId());//业务id
                            gcgMsg2.setBusinessType("工程");//业务类型
                            gcgMsg2.setBusinessName(dm);//业务名称
                            gcgMsg2.setTaskId(status.getTaskId());//任务id
                            gcgMsg2.setMsgType("可乐机已确认");//消息类型
                            gcgMsg2.setMessage(data);//消息内容
                            gcgMsg2.setPath(url);//跳转路径
                            gcgMsg2.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                            gcgMsg2.setSendUser(status.getCreateId());//发送人id
                            gcgMsg2.setReceiveUser(user.getUserId());//接收人ID
                            gcgMsg2.setCreateTime(new Date());//创建时间

                            //消息表新增可乐机已确认消息
                            this.baseMapper.insert(gcgMsg2);


                            //股份未交齐
                        } else if (status.getTaskId() == 18) {

                            GcgMsg gcgMsg = new GcgMsg();
                            gcgMsg.setBusinessId(status.getProId());//业务id
                            gcgMsg.setTaskId(status.getTaskId());//任务id
                            gcgMsg.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                            gcgMsg.setEndTime(new Date());//任务完成时间

                            gcgMsgs.add(gcgMsg);

                            //网上建店
                        } else if (status.getTaskId() == 36) {

                            GcgMsg gcgMsg = new GcgMsg();
                            gcgMsg.setBusinessId(status.getProId());//业务id
                            gcgMsg.setTaskId(status.getTaskId());//任务id
                            gcgMsg.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                            gcgMsg.setEndTime(new Date());//任务完成时间

                            gcgMsgs.add(gcgMsg);

                            //线上套餐
                        } else if (status.getTaskId() == 37) {

                            GcgMsg gcgMsg = new GcgMsg();
                            gcgMsg.setBusinessId(status.getProId());//业务id
                            gcgMsg.setTaskId(status.getTaskId());//任务id
                            gcgMsg.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                            gcgMsg.setEndTime(new Date());//任务完成时间

                            gcgMsgs.add(gcgMsg);

                            //排队机
                        } else if (status.getTaskId() == 38) {

                            GcgMsg gcgMsg = new GcgMsg();
                            gcgMsg.setBusinessId(status.getProId());//业务id
                            gcgMsg.setTaskId(status.getTaskId());//任务id
                            gcgMsg.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                            gcgMsg.setEndTime(new Date());//任务完成时间

                            gcgMsgs.add(gcgMsg);

                            //当地供货商
                        } else if (status.getTaskId() == 17) {

                            GcgMsg gcgMsg = new GcgMsg();
                            gcgMsg.setBusinessId(status.getProId());//业务id
                            gcgMsg.setTaskId(status.getTaskId());//任务id
                            gcgMsg.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                            gcgMsg.setEndTime(new Date());//任务完成时间

                            gcgMsgs.add(gcgMsg);

                            //宿舍信息
                        } else if (status.getTaskId() == 23) {

                            GcgMsg gcgMsg = new GcgMsg();
                            gcgMsg.setBusinessId(status.getProId());//业务id
                            gcgMsg.setTaskId(status.getTaskId());//任务id
                            gcgMsg.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                            gcgMsg.setEndTime(new Date());//任务完成时间

                            gcgMsgs.add(gcgMsg);

                            //卫生许可证
                        } else if (status.getTaskId() == 13) {

                            GcgMsg gcgMsg = new GcgMsg();
                            gcgMsg.setBusinessId(status.getProId());//业务id
                            gcgMsg.setTaskId(status.getTaskId());//任务id
                            gcgMsg.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                            gcgMsg.setEndTime(new Date());//任务完成时间

                            gcgMsgs.add(gcgMsg);
                        }

                        //确定工程开业日期
                        else if (status.getTaskId() == 16) {

                            GcgMsg gcgMsg = new GcgMsg();
                            gcgMsg.setBusinessId(status.getProId());//业务id
                            gcgMsg.setTaskId(status.getTaskId());//任务id
                            gcgMsg.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                            gcgMsg.setEndTime(new Date());//任务完成时间

                            gcgMsgs.add(gcgMsg);

                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            Date toDate = sdf.parse(status.getContent());//开业日期

                            //工程表
                            ProjectParam projectParam = new ProjectParam();
                            projectParam.setOpenDate(toDate);//开业日期
                            projectParam.setId(status.getProId());

                            //修改工程开业时间
                            projectMapper.updateProjectByCondition(projectParam);
                            logger.info("===========工程表已添加开业时间========");

                            //收银系统负责人 (刘书伟)
                            String syxt = BusConst.LSW;
                            String[] openids = new String[]{syxt};

                            //收银系统负责人 (刘书伟)id
                            User user = btyWxMapper.selectUser(syxt);
                            Map<String, String> openidMap = projectService.selectDd(status.getProId());

                            String ddName = openidMap.get("ddName");
                            String ddTel = openidMap.get("ddTel");

                            //推送收银系统确认消息
                            String data = dm + "已确定开业日期为: " + status.getContent() + ",请确认收银系统。该店督导为：" + ddName + "电话为: " + ddTel;
                            logger.info("==========开业时间已确认========");

                            JSONObject tempObj = weChatSendTempMsgService.setTempParam(dm, null, "确认收银系统", "收银系统", DateUtil.getNow(), data);

                            if (ToolUtil.isNotEmpty(user)){
                                //收银系统url(负责人)
                                String syUrl = BusConst.SYURL + "&proId=" + status.getProId() + "&taskId=21" + "&userId=" + user.getUserId();
                                String url = path + gzhConfig.getAppid() + "&redirect_uri=" + URLEncoder.encode(syUrl) + type + scope + state;

                                //推送收银系统消息
                                weChatSendTempMsgService.sendmsg(openids, url, tempObj);
                                logger.info("===========推送收银系统负责人消息==========");

                                //消息表中新增收银系统消息
                                GcgMsg gcgMsg1 = new GcgMsg();
                                gcgMsg1.setBusinessId(status.getProId());//业务id
                                gcgMsg1.setBusinessType("工程");//业务类型
                                gcgMsg1.setBusinessName(dm);//业务名称
                                gcgMsg1.setTaskId(21);//任务id
                                gcgMsg1.setMsgType("确认收银系统提醒");//消息类型
                                gcgMsg1.setMessage(data);//消息内容
                                gcgMsg1.setPath(url);//跳转路径
                                gcgMsg1.setTaskStatus(1);//任务状态;0->已完成，1->未完成
                                gcgMsg1.setSendUser(status.getCreateId());//发送人id
                                gcgMsg1.setReceiveUser(user.getUserId());//接收人ID
                                gcgMsg1.setCreateTime(new Date());//创建时间

                                //消息表新增收银系统数据
                                this.baseMapper.insert(gcgMsg1);
                            }

                            String data1 = dm + "已确定开业日期:" + status.getContent() + "。该店督导为：" + ddName + "电话为: " + ddTel;
                            JSONObject tempObj1 = weChatSendTempMsgService.setTempParam(dm, null, "确认开业日期", "开业日期", DateUtil.getNow(), data1);

                            //设计总监(朱小平)
                            String sjzj = BusConst.SJZJ;
                            String[] openids1 = new String[]{sjzj};
                            //设计总监(朱小平)id
                            User user1 = btyWxMapper.selectUser(sjzj);

                            if (ToolUtil.isNotEmpty(user1)){

                                //确定开业时间url(已确认)
                                String kyUrl = BusConst.KYURL + "&proId=" + status.getProId() + "&taskId=" + status.getTaskId() + "&userId=" + user1.getUserId();
                                String url1 = path + gzhConfig.getAppid() + "&redirect_uri=" + URLEncoder.encode(kyUrl) + type + scope + state;

                                //推送设计总监消息
                                weChatSendTempMsgService.sendmsg(openids1, url1, tempObj1);
                                logger.info("========推送设计总监消息==========");

                                //消息表中新增确认开业时间消息
                                GcgMsg gcgMsg2 = new GcgMsg();
                                gcgMsg2.setBusinessId(status.getProId());//业务id
                                gcgMsg2.setBusinessType("工程");//业务类型
                                gcgMsg2.setBusinessName(dm);//业务名称
                                gcgMsg2.setTaskId(16);//任务id
                                gcgMsg2.setMsgType("已确认开业日期提醒");//消息类型
                                gcgMsg2.setMessage(data1);//消息内容
                                gcgMsg2.setPath(url1);//跳转路径
                                gcgMsg2.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                                gcgMsg2.setSendUser(status.getCreateId());//发送人id
                                gcgMsg2.setReceiveUser(user1.getUserId());//接收人ID
                                gcgMsg2.setCreateTime(new Date());//创建时间

                                //消息表新增开业日期提醒
                                this.baseMapper.insert(gcgMsg2);
                            }

                            //丁玲
                            String dl = BusConst.DL;
                            String[] openid = new String[]{dl};
                            //丁玲id
                            User userDl = btyWxMapper.selectUser(dl);

                            if (ToolUtil.isNotEmpty(userDl)){
                                //推送丁玲消息
                                weChatSendTempMsgService.sendmsg(openid, BusConst.MSURL, tempObj1);
                                logger.info("=======推送丁玲消息==========");

                                //消息表中新增确认开业时间消息
                                GcgMsg msg1 = new GcgMsg();
                                msg1.setBusinessId(status.getProId());//业务id
                                msg1.setBusinessType("工程");//业务类型
                                msg1.setBusinessName(dm);//业务名称
                                msg1.setTaskId(status.getTaskId());//任务id
                                msg1.setMsgType("已确认开业日期提醒");//消息类型
                                msg1.setMessage(data1);//消息内容
                                msg1.setPath(BusConst.MSURL);//跳转路径
                                msg1.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                                msg1.setSendUser(status.getCreateId());//发送人id
                                msg1.setReceiveUser(userDl.getUserId());//接收人ID
                                msg1.setCreateTime(new Date());//创建时间

                                //消息表新增收确认开业时间消息
                                this.baseMapper.insert(msg1);
                            }

                            //确认收银系统
                        } else if (status.getTaskId() == 21) {

                            if (status.getContent().equals("收银账号已建完，收银档案未建完")) {

                                GcgMsg gcgMsg = new GcgMsg();
                                gcgMsg.setBusinessId(status.getProId());//业务id
                                gcgMsg.setTaskId(status.getTaskId());//任务id
                                gcgMsg.setTaskStatus(1);//任务状态;0->已完成，1->未完成
                                gcgMsg.setEndTime(new Date());//任务完成时间

                                gcgMsgs.add(gcgMsg);

                                taskService.addTxl(status.getProId(), null, status.getTaskId());

//                                if (ToolUtil.isNotEmpty(project)) {

//                                    //店名（截取省之后的字符）
//                                    String btyDm = project.getName().split(project.getProvinceName())[1].replaceAll("\\s*", "");
//
//                                    //通讯录新增数据
//                                    BtyTxl btyTxl = new BtyTxl();
//                                    btyTxl.setCsqy(project.getProvinceName());//城市区域
//                                    btyTxl.setYyjlqy(project.getYyjlName());//营运经理
//                                    btyTxl.setDm(btyDm);//店名
//                                    btyTxl.setKysj(project.getOpenTime());//开业时间
//                                    btyTxl.setCtdz(project.getRestAddress());//餐厅地址
//                                    btyTxlService.addct(btyTxl);
//
//
//                                    //查询表中是否存在该数据
//                                    int ddCount = btyTxlMapper.selectCount(btyDm, "督导");
//
//                                    //bty_txl_ry表新增数据
//                                    BtyTxlRY btyTxlRY = new BtyTxlRY();
//                                    btyTxlRY.setZw("督导");//职务(督导)
//                                    btyTxlRY.setFzr(project.getDdName());//负责人
//                                    btyTxlRY.setLxfs(project.getDdTel());//联系方式
//                                    btyTxlRY.setSsctdm(btyDm);//店名
//
//                                    //不存在该数据
//                                    if (ddCount == 0) {
//                                        btyTxlMapper.addfzr(btyTxlRY);
//                                    } else {
//                                        btyTxlMapper.updateTxlRY(btyTxlRY);
//                                    }
//
//                                    //查询表中是否存在该数据
//                                    int dzCount = btyTxlMapper.selectCount(btyDm, "店长");
//
//                                    //bty_txl_ry表新增数据
//                                    BtyTxlRY btyTxlRY1 = new BtyTxlRY();
//                                    btyTxlRY1.setZw("店长");//职务(店长)
//                                    btyTxlRY1.setFzr(project.getDz());//负责人
//                                    btyTxlRY1.setLxfs(project.getDzTel());//联系方式
//                                    btyTxlRY1.setSsctdm(btyDm);//店名
//
//                                    //不存在该数据
//                                    if (dzCount == 0) {
//                                        btyTxlMapper.addfzr(btyTxlRY1);
//                                    } else {
//                                        btyTxlMapper.updateTxlRY(btyTxlRY1);
//                                    }
//
//                                    //btyy_region表新增数据
//                                    BtyRegion btyRegion = new BtyRegion();
//                                    btyRegion.setYyjl(project.getYyjlName());//营运经理
//                                    btyRegion.setDm(btyDm);//店名
//                                    btyRegion.setDd(project.getDdName());//督导
//                                    btyRegion.setDz(project.getDz());//店长
//                                    btyRegion.setCreateTime(new Date());
//                                    this.regionMapper.insert(btyRegion);
//                                }


                                //通知督导,韩铭渲
                                String now = DateUtil.getNow();//当前时间
                                String data = dm + "收银账号已建完";

                                JSONObject tempObj = weChatSendTempMsgService.setTempParam(dm, null, "收银账号已建完", "收银账号已建完", now, data);

                                Map<String, String> openidMap = projectService.selectDd(status.getProId());
                                //督导
                                String dd = openidMap.get("dd");

                                List list = new ArrayList();
                                list.add(dd);

                                //接收消息人id
                                List<User> userList = btyWxMapper.getUser(list);
                                if (ToolUtil.isNotEmpty(userList)) {
                                    for (User user : userList) {

                                        //收银系统url(督导)
                                        String syUrl = BusConst.SYDDURL + "&proId=" + status.getProId() + "&taskId=" + status.getTaskId() + "&userId=" + user.getUserId();
                                        String url = path + gzhConfig.getAppid() + "&redirect_uri=" + URLEncoder.encode(syUrl) + type + scope + state;

                                        String[] openids = new String[]{user.getOpenid()};

                                        //推送消息
                                        weChatSendTempMsgService.sendmsg(openids, url, tempObj);
                                        logger.info("======已推送督导收银账号已建完消息======");


                                        //消息表中新增收银系统消息
                                        GcgMsg gcgMsg1 = new GcgMsg();
                                        gcgMsg1.setBusinessId(status.getProId());//业务id
                                        gcgMsg1.setBusinessType("工程");//业务类型
                                        gcgMsg1.setBusinessName(dm);//业务名称
                                        gcgMsg1.setTaskId(status.getTaskId());//任务id
                                        gcgMsg1.setMsgType("收银账号已建完");//消息类型
                                        gcgMsg1.setMessage(data);//消息内容
                                        gcgMsg1.setPath(url);//跳转路径
                                        gcgMsg1.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                                        gcgMsg1.setSendUser(status.getCreateId());//发送人id
                                        gcgMsg1.setReceiveUser(user.getUserId());//接收人ID
                                        gcgMsg1.setCreateTime(new Date());//创建时间

                                        //消息表新增收银系统消息
                                        this.baseMapper.insert(gcgMsg1);
                                    }
                                }

                                //收银档案负责人（韩铭渲）
                                String sy = BusConst.HMX;
                                String[] openid = new String[]{sy};
                                User user1 = btyWxMapper.selectUser(sy);

                                if (ToolUtil.isNotEmpty(user1)){
                                    //收银档案负责人url
                                    String syUrl = BusConst.DAURL + "&proId=" + status.getProId() + "&taskId=" + status.getTaskId() + "&userId=" + user1.getUserId();
                                    String url1 = path + gzhConfig.getAppid() + "&redirect_uri=" + URLEncoder.encode(syUrl) + type + scope + state;

                                    //推送收银档案负责人消息
                                    weChatSendTempMsgService.sendmsg(openid, url1, tempObj);
                                    logger.info("======已推送收银档案负责人收银账号已建完消息======");

                                    //消息表中新增收银系统消息
                                    GcgMsg gcgMsg2 = new GcgMsg();
                                    gcgMsg2.setBusinessId(status.getProId());//业务id
                                    gcgMsg2.setBusinessType("工程");//业务类型
                                    gcgMsg2.setBusinessName(dm);//业务名称
                                    gcgMsg2.setTaskId(status.getTaskId());//任务id
                                    gcgMsg2.setMsgType("收银账号已建完");//消息类型
                                    gcgMsg2.setMessage(data);//消息内容
                                    gcgMsg2.setPath(url1);//跳转路径
                                    gcgMsg2.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                                    gcgMsg2.setSendUser(status.getCreateId());//发送人id
                                    gcgMsg2.setReceiveUser(user1.getUserId());//接收人ID
                                    gcgMsg2.setCreateTime(new Date());//创建时间

                                    //消息表新增收银系统消息
                                    this.baseMapper.insert(gcgMsg2);
                                }

                            }//else if (status.getContent().equals("收银档案已建完")){

                           // }

                        }
                        //卫生许可证
                        else if (status.getTaskId() == 44) {

                            taskService.addTxl(status.getProId(), status.getContent(), status.getTaskId());
//
//                            //获取妖码
//                            String yaoma = projectMapper.getYm(status.getProId());
//
//                            //通讯录同步食品经营许可证数据
//                            BtyTxl btyTxl = new BtyTxl();
//                            btyTxl.setSpjyxk(status.getContent());//食品经营许可证
//                            btyTxl.setCtbh(yaoma);//妖码
//                            btyTxlService.edit(btyTxl);
                        }
                        //上传营业执照
                        else if (status.getTaskId() == 11) {

                            GcgMsg gcgMsg = new GcgMsg();
                            gcgMsg.setBusinessId(status.getProId());//业务id
                            gcgMsg.setTaskId(status.getTaskId());//任务id
                            gcgMsg.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                            gcgMsg.setEndTime(new Date());//任务完成时间

                            gcgMsgs.add(gcgMsg);


                            TaskStatus status1 = new TaskStatus();
                            //可乐机报备
                            status1.setTaskId(19);
                            status1.setProId(status.getProId());
                            status1.setStatus(0);
                            status1.setTaskName("可乐机");
                            status1.setContent("可乐机已报备");
                            status1.setCreateId(status.getCreateId());

                            taskStatuses.add(status1);

                            //流程明细表
                            TaskFlow taskFlow1 = new TaskFlow();
                            taskFlow1.setProId(status.getProId());
                            taskFlow1.setTaskId(19);
                            taskFlow1.setStatus(0);//1提交；0驳回
                            taskFlow1.setTaskName("可乐机");
                            taskFlow1.setCreateUser(status.getCreateId());
                            taskFlow1.setCreateTime(new Date());

                            taskFlows.add(taskFlow1);

                            //通知可乐机负责人
                            String now = DateUtil.getNow();//当前时间
                            String data = dm + "应入可乐机";

                            JSONObject tempObj = weChatSendTempMsgService.setTempParam(dm, null, "可乐机状态", "可乐机", now, data);

                            Long role = 1314488482857709570L;//可乐机负责人
                            List<String> openidlist = userService.listOpenidsByRole(role);

                            if (ToolUtil.isNotEmpty(openidlist)){
                                logger.info("=========可乐机负责人:" + openidlist + "========");

                                //可乐机负责人id
                                List<User> user1 = btyWxMapper.getUser(openidlist);

                                if (ToolUtil.isNotEmpty(user1)) {
                                    for (User user : user1) {

                                        //可乐机url
                                        String kljUrl = BusConst.KLJURL + "&proId=" + status.getProId() + "&taskId=19" + "&userId=" + user.getUserId();
                                        String url = path + gzhConfig.getAppid() + "&redirect_uri=" + URLEncoder.encode(kljUrl) + type + scope + state;

                                        String[] openids = new String[]{user.getOpenid()};

                                        //推送消息
                                        weChatSendTempMsgService.sendmsg(openids, url, tempObj);
                                        logger.info("======已推送可乐机负责人消息======");


                                        //消息表中新增应入可乐机消息
                                        GcgMsg gcgMsg2 = new GcgMsg();
                                        gcgMsg2.setBusinessId(status.getProId());//业务id
                                        gcgMsg2.setBusinessType("工程");//业务类型
                                        gcgMsg2.setBusinessName(dm);//业务名称
                                        gcgMsg2.setTaskId(19);//任务id
                                        gcgMsg2.setMsgType("应入可乐机提醒");//消息类型
                                        gcgMsg2.setMessage(data);//消息内容
                                        gcgMsg2.setPath(url);//跳转路径
                                        gcgMsg2.setTaskStatus(1);//任务状态;0->已完成，1->未完成
                                        gcgMsg2.setSendUser(status.getCreateId());//发送人id
                                        gcgMsg2.setReceiveUser(user.getUserId());//接收人ID
                                        gcgMsg2.setCreateTime(new Date());//创建时间

                                        //消息表新增可乐机消息
                                        this.baseMapper.insert(gcgMsg2);
                                    }
                                }
                            }

                            //刘书伟
                            String syxt = BusConst.LSW;
                            String[] openids = new String[]{syxt};
                            //收银系统负责人 (刘书伟)id
                            User user = btyWxMapper.selectUser(syxt);
                            Map<String, String> openidMap = projectService.selectDd(status.getProId());

                            String ddName = openidMap.get("ddName");
                            String ddTel = openidMap.get("ddTel");

                            //推送收银系统确认消息
                            String data1 = dm + "已填写营业执照信息，请确认收银系统。该店督导为：" + ddName + "电话为: " + ddTel;
                            logger.info("==========已填写营业执照信息========");

                            JSONObject tempObj1 = weChatSendTempMsgService.setTempParam(dm, null, "确认收银系统", "收银系统", DateUtil.getNow(), data1);

                            if (ToolUtil.isNotEmpty(user)){
                                //收银系统url(负责人)
                                String syUrl = BusConst.SYURL + "&proId=" + status.getProId() + "&taskId=21" + "&userId=" + user.getUserId();
                                String url = path + gzhConfig.getAppid() + "&redirect_uri=" + URLEncoder.encode(syUrl) + type + scope + state;

                                //推送收银系统消息
                                weChatSendTempMsgService.sendmsg(openids, url, tempObj1);
                                logger.info("===========推送收银系统负责人消息==========");

                                //消息表中新增收银系统消息
                                GcgMsg gcgMsg1 = new GcgMsg();
                                gcgMsg1.setBusinessId(status.getProId());//业务id
                                gcgMsg1.setBusinessType("工程");//业务类型
                                gcgMsg1.setBusinessName(dm);//业务名称
                                gcgMsg1.setTaskId(21);//任务id
                                gcgMsg1.setMsgType("确认收银系统提醒");//消息类型
                                gcgMsg1.setMessage(data);//消息内容
                                gcgMsg1.setPath(url);//跳转路径
                                gcgMsg1.setTaskStatus(1);//任务状态;0->已完成，1->未完成
                                gcgMsg1.setSendUser(status.getCreateId());//发送人id
                                gcgMsg1.setReceiveUser(user.getUserId());//接收人ID
                                gcgMsg1.setCreateTime(new Date());//创建时间

                                //消息表新增收银系统数据
                                this.baseMapper.insert(gcgMsg1);
                            }

                            //配送要货完成
                        } else if (status.getTaskId() == 15) {

                            status.setStatus(0);//待发货
                            taskStatuses.add(status);

                            GcgMsg gcgMsg = new GcgMsg();
                            gcgMsg.setBusinessId(status.getProId());//业务id
                            gcgMsg.setTaskId(status.getTaskId());//任务id
                            gcgMsg.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                            gcgMsg.setEndTime(new Date());//任务完成时间

                            gcgMsgs.add(gcgMsg);

                            // 工程表
                            ProjectParam projectParam = new ProjectParam();
                            projectParam.setArrivalTime(status.getContent());//到货日期
                            projectParam.setId(status.getProId());
                            projectParam.setPsStatus("0");//待发货

                            //修改工程到货日期
                            projectMapper.updateProjectByCondition(projectParam);
                            logger.info("===========工程表已添加到货日期========");


                            //推送供应链消息
                            String data = dm + "已确认到货日期为" + status.getContent() + "，请尽快完成配送！";
                            logger.info("==========已完成配送========");

                            JSONObject tempObj = weChatSendTempMsgService.setTempParam(dm, null, "配送要货", "配送要货", DateUtil.getNow(), data);

//                            Long role = 1314441729702100993L;//供应链
                            Long role = 1278615893124055041L;//配送管理
                            List<String> list = userService.listOpenidsByRole(role);

                            if (ToolUtil.isNotEmpty(list)){
                                logger.info("=========配送管理:" + list + "========");

                                String[] openids = list.toArray(new String[list.size()]);
                                //推送消息
                                weChatSendTempMsgService.sendmsg(openids, BusConst.MSURL, tempObj);
                                logger.info("======推送配送管理消息======");
                            }


//                            //配送管理id
//                            List<User> user1 = btyWxMapper.getUser(list);
//
//                            if (ToolUtil.isNotEmpty(user1)) {
//                                for (User user : user1) {
//
//                                    //配送要货url
//                                    String psyhUrl = BusConst.PSYHURL + "&proId=" + status.getProId() + "&taskId=" + status.getTaskId() + "&userId=" + user.getUserId();
//                                    String url = path + gzhConfig.getAppid() + "&redirect_uri=" + URLEncoder.encode(psyhUrl) + type + scope + state;
//
//                                    String[] openids = new String[]{user.getOpenid()};
//
//                                    //推送消息
//                                    weChatSendTempMsgService.sendmsg(openids, url, tempObj);
//                                    logger.info("======推送配送管理消息======");
//
//
//                                    //消息表中新增配送要货消息
//                                    GcgMsg gcgMsg2 = new GcgMsg();
//                                    gcgMsg2.setBusinessId(status.getProId());//业务id
//                                    gcgMsg2.setBusinessType("工程");//业务类型
//                                    gcgMsg2.setBusinessName(dm);//业务名称
//                                    gcgMsg2.setTaskId(status.getTaskId());//任务id
//                                    gcgMsg2.setMsgType("推送配送管理消息");//消息类型
//                                    gcgMsg2.setMessage(data);//消息内容
//                                    gcgMsg2.setPath(url);//跳转路径
//                                    gcgMsg2.setTaskStatus(0);//任务状态;0->已完成，1->未完成
//                                    gcgMsg2.setSendUser(status.getCreateId());//发送人id
//                                    gcgMsg2.setReceiveUser(user.getUserId());//接收人ID
//                                    gcgMsg2.setCreateTime(new Date());//创建时间
//
//                                    //消息表新增配送要货提醒
//                                    this.baseMapper.insert(gcgMsg2);
//                                }
//                            }

                        }

                    }

                }
            }

            if (taskStatuses.size() != 0) {
                //新增信息
                count = taskMapper.addRentList(taskStatuses);
                result.append(count > 0 ? 1 : 0);
            }

            if (taskFlows.size() != 0) {
                //新增流程明细表
                count = taskFlowMapper.addList(taskFlows);
                result.append(count > 0 ? 1 : 0);
            }

            if (gcgMsgs.size() != 0) {
                //修改消息表
                count = this.baseMapper.updateList(gcgMsgs);
//                result.append(count > 0 ? 1 : 0);
            }

        }
        if (result.indexOf("0") >= 0) {
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        } else {
            return 1;
        }
    }


    /**
     * 收银系统建档
     * @param taskStatus
     * @return
     */
    @Override
    @Transactional
    public ResponseData archives(List<TaskStatus> taskStatus) {

        //修改状态,所有修改操作数据库新增条数必须都大于0才会提交事务
        StringBuilder result = new StringBuilder();
        // 数据库修改条数
        int count = 0;

        if (ToolUtil.isNotEmpty(taskStatus)) {

            for (TaskStatus status : taskStatus) {

                //流程明细表
                TaskFlow taskFlow = new TaskFlow();
                taskFlow.setProId(status.getProId());
                taskFlow.setTaskId(status.getTaskId());
                taskFlow.setStatus(2);//状态 1提交/确认/通过；0驳回; 2收银已建档
                if (status.getTaskName() != null || status.getTaskName() != "") {
                    taskFlow.setTaskName(status.getTaskName());
                }
                taskFlow.setCreateUser(status.getCreateId());
                taskFlow.setCreateTime(new Date());

                if (status.getStatus() == null) {
                    status.setStatus(2);//状态 1提交/确认/通过；0驳回/未完成；2收银已建档
                }

                //status表修改content
                count = taskMapper.updateCon(status);
                result.append(count > 0 ? 1 : 0);

                //flow表新增数据
                count = taskFlowMapper.add(taskFlow);
                result.append(count > 0 ? 1 : 0);

                //收银系统档案已建完
                if (status.getTaskId() == 21 && status.getContent().equals("收银档案已建完")) {

                    //通知督导，设计
                    String now = DateUtil.getNow();//当前时间
                    //查询店名
                    String dm = taskMapper.selectDm(status.getProId());
                    String data = dm + "收银档案已建完";

                    JSONObject tempObj = weChatSendTempMsgService.setTempParam(dm, null, "收银档案已建完", "收银档案已建完", now, data);

                    Map<String, String> openidMap = projectService.selectDd(status.getProId());
                    //督导
                    String dd = openidMap.get("dd");

                    User user = btyWxMapper.selectUser(dd);

                    //收银系统url(督导)
                    String syUrl = BusConst.SYDDURL + "&proId=" + status.getProId() + "&taskId=" + status.getTaskId() + "&userId=" + user.getUserId();
                    String url = path + gzhConfig.getAppid() + "&redirect_uri=" + URLEncoder.encode(syUrl) + type + scope + state;

                    String[] openids = new String[]{user.getOpenid()};

                    //推送消息
                    weChatSendTempMsgService.sendmsg(openids, url, tempObj);
                    logger.info("======已推送督导收银档案已建完消息======");


                    //消息表中新增收银系统消息
                    GcgMsg gcgMsg1 = new GcgMsg();
                    gcgMsg1.setBusinessId(status.getProId());//业务id
                    gcgMsg1.setBusinessType("工程");//业务类型
                    gcgMsg1.setBusinessName(dm);//业务名称
                    gcgMsg1.setTaskId(status.getTaskId());//任务id
                    gcgMsg1.setMsgType("收银档案已建完");//消息类型
                    gcgMsg1.setMessage(data);//消息内容
                    gcgMsg1.setPath(url);//跳转路径
                    gcgMsg1.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                    gcgMsg1.setSendUser(status.getCreateId());//发送人id
                    gcgMsg1.setReceiveUser(user.getUserId());//接收人ID
                    gcgMsg1.setCreateTime(new Date());//创建时间

                    //消息表新增收银系统消息
                    this.baseMapper.insert(gcgMsg1);


                    //设计(任召栋)
                    String sj = BusConst.SJ;
                    //设计(任召栋)
                    String[] openid = new String[]{sj};

                    //设计(任召栋)
                    User user1 = btyWxMapper.selectUser(sj);

                    if (ToolUtil.isNotEmpty(user1)){
                        //收银系统url（设计）
                        String sjUrl = BusConst.SYSJURL + "&proId=" + status.getProId() + "&taskId=" + status.getTaskId() + "&userId=" + user1.getUserId();
                        String url1 = path + gzhConfig.getAppid() + "&redirect_uri=" + URLEncoder.encode(sjUrl) + type + scope + state;

                        String ddName = openidMap.get("ddName");
                        String ddTel = openidMap.get("ddTel");
                        String data1 = dm + "收银档案已建完。请完成桌边码后联系督导：" + ddName + "，电话为:" + ddTel;
                        JSONObject tempObj1 = weChatSendTempMsgService.setTempParam(dm, null, "收银档案已建完", "收银档案已建完", now, data1);

                        //推送设计消息
                        weChatSendTempMsgService.sendmsg(openid, url1, tempObj1);
                        logger.info("======已推送设计收银档案已建完消息======");

                        //消息表中新增收银系统消息
                        GcgMsg gcgMsg2 = new GcgMsg();
                        gcgMsg2.setBusinessId(status.getProId());//业务id
                        gcgMsg2.setBusinessType("工程");//业务类型
                        gcgMsg2.setBusinessName(dm);//业务名称
                        gcgMsg2.setTaskId(status.getTaskId());//任务id
                        gcgMsg2.setMsgType("收银档案已建完");//消息类型
                        gcgMsg2.setMessage(data);//消息内容
                        gcgMsg2.setPath(url1);//跳转路径
                        gcgMsg2.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                        gcgMsg2.setSendUser(status.getCreateId());//发送人id
                        gcgMsg2.setReceiveUser(user1.getUserId());//接收人ID
                        gcgMsg2.setCreateTime(new Date());//创建时间

                        //消息表新增收银系统消息
                        this.baseMapper.insert(gcgMsg2);
                    }

                    Long role = 1350308851581628418L;//技术运维
                    List<String> openidlist = userService.listOpenidsByRole(role);

                    if (ToolUtil.isNotEmpty(openidlist)){
                        logger.info("=========技术运维:" + openidlist + "========");
                        //技术运维id
                        List<User> ywUser = btyWxMapper.getUser(openidlist);

                        String data2 = dm + "收银档案已建完，请生成二维码联系设计部。";
                        JSONObject tempObj2 = weChatSendTempMsgService.setTempParam(dm, null, "收银档案已建完", "收银档案已建完", now, data2);

                        if (ToolUtil.isNotEmpty(ywUser)) {
                            for (User yw : ywUser) {

                                String[] ywOpenid = new String[]{yw.getOpenid()};

                                //推送消息
                                weChatSendTempMsgService.sendmsg(ywOpenid, null, tempObj2);
                                logger.info("======已推送技术运维消息======");

                                //消息表中新增收银档案已建完消息
                                GcgMsg gcgMsg = new GcgMsg();
                                gcgMsg.setBusinessId(status.getProId());//业务id
                                gcgMsg.setBusinessType("工程");//业务类型
                                gcgMsg.setBusinessName(dm);//业务名称
                                gcgMsg.setTaskId(status.getTaskId());//任务id
                                gcgMsg.setMsgType("收银档案已建完");//消息类型
                                gcgMsg.setMessage(data);//消息内容
                                gcgMsg.setTaskStatus(0);//任务状态;0->已完成，1->未完成
                                gcgMsg.setSendUser(status.getCreateId());//发送人id
                                gcgMsg.setReceiveUser(yw.getUserId());//接收人ID
                                gcgMsg.setCreateTime(new Date());//创建时间

                                //消息表新增收银档案已建完消息
                                this.baseMapper.insert(gcgMsg);
                            }
                        }
                    }
            }

            }
        }
        if (result.indexOf("0") >= 0) {
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseData.error("修改失败");
        } else {
            return ResponseData.success();
        }

    }


    /**
     * 查看当前任务信息
     *
     * @param proId
     * @param taskId
     */
    @Override
    public List<TaskStatus> taskList(int proId, int taskId, String wxOpenId, Long userId) {

        logger.info("===============" + proId + "==" + "--" + taskId + "--" + userId + "==========");

        List<TaskStatus> list = new ArrayList<>();
        List taskIdList = new ArrayList<>();

        //查询当前登录者是否记录有电话
        String phone = null;
        if (ToolUtil.isEmpty(userId)) {
            phone = btyWxMapper.selectPhone(wxOpenId);
        } else {
            phone = userMapper.getPhone(userId);
        }

        String roleIds = null;
        if (phone != null && !"".equals(phone)) {

            //根据wxOpenId查询当前登陆者角色
            Map map = btyWxMapper.getRole(phone);
            if (map != null && map.size() != 0) {
                roleIds = map.get("roleId").toString();//获取当前登录者角色
            }
        }
        //设计、收银账号负责人查看收银系统
//        if ((roleIds.contains("1352167937466048514") || roleIds.contains("1310462425628737538")) && taskId == 21) {
//        收银账号负责人查看收银系统
        if (roleIds.contains("1310462425628737538") && taskId == 21) {
            //营业执照和开业时间
            taskIdList.add(11);
            taskIdList.add(42);
            taskIdList.add(45);

            taskIdList.add(56);
            taskIdList.add(57);
            taskIdList.add(58);

            taskIdList.add(46);
            taskIdList.add(47);
            taskIdList.add(55);
            taskIdList.add(16);
            taskIdList.add(21);


            //可乐机负责人查看可乐机
        } else if (roleIds.contains("1314488482857709570")) {

            //可乐机，营业执照和开业时间
            taskIdList.add(11);
            taskIdList.add(42);
            taskIdList.add(45);

            taskIdList.add(56);
            taskIdList.add(57);
            taskIdList.add(58);

            taskIdList.add(46);
            taskIdList.add(47);
            taskIdList.add(55);
            taskIdList.add(19);
            taskIdList.add(16);

        } else {

            //营业执照
            if (taskId == 11) {

                taskIdList.add(11);
                taskIdList.add(42);
                taskIdList.add(45);

                taskIdList.add(56);
                taskIdList.add(57);
                taskIdList.add(58);

                taskIdList.add(46);
                taskIdList.add(47);
                taskIdList.add(55);

                //宿舍信息
            } else if (taskId == 23) {

                taskIdList.add(23);
                taskIdList.add(24);
                taskIdList.add(25);
                taskIdList.add(26);
                taskIdList.add(27);
                taskIdList.add(29);

                //卫生许可证
            } else if (taskId == 13) {

                taskIdList.add(13);
                taskIdList.add(44);

                //网上建店
            } else if (taskId == 36) {

                taskIdList.add(36);
                taskIdList.add(37);
                taskIdList.add(38);

            } else {
                taskIdList.add(taskId);
            }
        }

        logger.info("]]]]]]]]]]]]]]]" + taskIdList);
        list = this.baseMapper.taskList(proId, taskIdList);

        return list;
    }


    /**
     * 新增任务信息(工程)
     *
     * @param taskStatus
     * @return
     */
    @Override
    @Transactional
    public int addPro(List<TaskStatus> taskStatus) throws ParseException {
        logger.info("==========" + taskStatus);

        //修改状态,所有修改操作数据库新增条数必须都大于0才会提交事务
        StringBuilder result = new StringBuilder();
        // 数据库修改条数
        int count = 0;
        List<WxPushParamObj> pushList = new ArrayList<>();

        if (ToolUtil.isNotEmpty(taskStatus)) {

            //任务状态
            List<TaskStatus> taskStatuses = new ArrayList<>();
            //任务流水
            List<TaskFlow> taskFlows = new ArrayList<>();
            //消息管理
            List<GcgMsg> gcgMsgs = new ArrayList<>();

            for (TaskStatus status : taskStatus) {

                //填写内容为固定项
                if (status.getTaskId() != null) {

                    int msg = taskMapper.getRent(status.getProId(), status.getTaskId());

                    //之前没有填写过信息
                    if (msg == 0) {

                        if (status.getStatus() == null) {
                            status.setStatus(1);//提交信息
                        }


                        //不是出图日期新增
                        if (status.getTaskId() != 31 && ToolUtil.isNotEmpty(status.getContent())){
                            taskStatuses.add(status);
                            //是出图日期修改状态
                        }else if (status.getTaskId() == 31) {
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            Date date = new Date();
                            String format = sdf.format(date);
                            status.setContent(format);//出图日期改为确定日期
                            taskMapper.updateStatus(status);
                        }

                        //流程明细表
                        TaskFlow taskFlow = new TaskFlow();
                        taskFlow.setProId(status.getProId());
                        taskFlow.setTaskId(status.getTaskId());

                        if (status.getStatus() != null) {
                            taskFlow.setStatus(status.getStatus());
                        } else {
                            taskFlow.setStatus(1);
                        }

                        if (status.getTaskName() != null || status.getTaskName() != "") {
                            taskFlow.setTaskName(status.getTaskName());
                        }
                        taskFlow.setCreateUser(status.getCreateId());
                        taskFlow.setCreateTime(new Date());

                        taskFlows.add(taskFlow);

                        //查询店名
                        String dm = taskMapper.selectDm(status.getProId());
                        //任务名称
                        String taskName = taskMapper.getName(status.getTaskId());

                        //现场踏勘
                        if (status.getTaskId() == 30){


                            //查询该店出图日期是否已经确认
                            int conCt = taskMapper.select(status.getProId(),31);
                            //没有出图日期
                            if (conCt == 0){
                                //当前日期
                                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                String date = df.format(new Date());

                                //当前日期+7
                                String seven = DateUtil.addDate(date, 7);
                                //当前日期+10
                                String ten = DateUtil.addDate(date, 10);
                                String con = seven + "~" + ten;

                                TaskStatus status1 = new TaskStatus();
                                //出图日期
                                status1.setTaskId(31);
                                status1.setProId(status.getProId());
                                status1.setStatus(0);
                                status1.setTaskName("出图日期");
                                status1.setContent(con);
                                status1.setCreateId(status.getCreateId());

                                taskStatuses.add(status1);

                                //流程明细表
                                TaskFlow taskFlow1 = new TaskFlow();
                                taskFlow1.setProId(status.getProId());
                                taskFlow1.setTaskId(31);
                                taskFlow1.setStatus(0);//1提交；2审核未通过；3审核通过
                                taskFlow1.setTaskName("出图日期");
                                taskFlow1.setCreateUser(status.getCreateId());
                                taskFlow1.setCreateTime(new Date());

                                taskFlows.add(taskFlow1);

                                //工程设计
                                Long gcsj = 1373822382028705794L;
                                List<String> openidlist = userService.listOpenidsByRole(gcsj);

                                if (ToolUtil.isNotEmpty(openidlist)){
                                    logger.info("=========工程设计:" + openidlist + "========");

                                    //工程设计id
                                    List<User> user1 = btyWxMapper.getUser(openidlist);

                                    if (ToolUtil.isNotEmpty(user1)) {
                                        for (User user : user1) {

                                            String[] openids = new String[]{user.getOpenid()};

                                            //推送工程设计消息
                                            String data = dm + "现场踏勘信息已提交，请及时完成设计图！";
                                            logger.info("==========现场踏勘信息已提交========");

                                            JSONObject tempObj = weChatSendTempMsgService.setTempParam(dm, null, "确认出图日期", "出图日期", DateUtil.getNow(), data);

                                            //出图日期url
                                            String syUrl = BusConst.WGURL + "&proId=" + status.getProId() + "&taskId=31" + "&userId=" + user.getUserId();
                                            String url = path + gzhConfig.getAppid() + "&redirect_uri=" + URLEncoder.encode(syUrl) + type + scope + state;

                                            pushList.add(new WxPushParamObj(openids,url,tempObj));

                                            //消息表中新增出图日期消息
                                            GcgMsg gcgMsg = new GcgMsg();
                                            gcgMsg.setBusinessId(status.getProId());//业务id
                                            gcgMsg.setBusinessType("工程");//业务类型
                                            gcgMsg.setBusinessName(dm);//业务名称
                                            gcgMsg.setTaskId(31);//任务id
                                            gcgMsg.setMsgType("填写出图日期提醒");//消息类型
                                            gcgMsg.setMessage(data);//消息内容
                                            gcgMsg.setPath(url);//跳转路径
                                            gcgMsg.setTaskStatus(1);//任务状态;0->已完成，1->未完成
                                            gcgMsg.setSendUser(status.getCreateId());//发送人id
                                            gcgMsg.setReceiveUser(user.getUserId());//接收人ID
                                            gcgMsg.setCreateTime(new Date());//创建时间

                                            //消息表新增出图日期消息
                                            this.baseMapper.insert(gcgMsg);
                                        }
                                    }
                                }
                            }

                            //开工日期
                        }else if (status.getTaskId() == 32){

                            TaskStatus status1 = new TaskStatus();
                            //预估完工日期
                            status1.setTaskId(33);
                            //开工日期+28天
                            status1.setContent(DateUtil.addDate(status.getContent(), 28));
                            status1.setProId(status.getProId());
                            status1.setStatus(1);
                            status1.setTaskName("预估完工日期");
                            status1.setCreateId(status.getCreateId());

                            taskStatuses.add(status1);

                            //流程明细表
                            TaskFlow taskFlow1 = new TaskFlow();
                            taskFlow1.setProId(status.getProId());
                            taskFlow1.setTaskId(33);
                            taskFlow1.setStatus(1);//1提交；0审核未通过
                            taskFlow1.setTaskName("预估完工日期");
                            taskFlow1.setCreateUser(status.getCreateId());
                            taskFlow1.setCreateTime(new Date());

                            taskFlows.add(taskFlow1);


                            //预估开业日期
                            TaskStatus sta = new TaskStatus();
                            //预估开业时间
                            sta.setTaskId(22);
                            //预估完工日期+7天
                            sta.setContent(DateUtil.addDate(status.getContent(), 35));
                            sta.setProId(status.getProId());
                            sta.setStatus(1);
                            sta.setTaskName("预估开业时间");
                            sta.setCreateId(status.getCreateId());

                            taskStatuses.add(sta);

                            //流程明细表
                            TaskFlow flow = new TaskFlow();
                            flow.setProId(status.getProId());
                            flow.setTaskId(22);
                            flow.setStatus(1);//1提交；0审核未通过
                            flow.setTaskName("预估开业时间");
                            flow.setCreateUser(status.getCreateId());
                            flow.setCreateTime(new Date());

                            taskFlows.add(flow);

                            //人事部
                            Long role = 1277443924781469698L;//人事部
                            List<String> openidlist = userService.listOpenidsByRole(role);
                            String[] rsOpenids = openidlist.toArray(new String[openidlist.size()]);
                            if (ToolUtil.isNotEmpty(rsOpenids)){
                                Map<String, String> openidMap = projectService.selectDd(status.getProId());
                                String ddName = openidMap.get("ddName");
                                String ddTel = openidMap.get("ddTel");

                                //推送人事部消息
                                String rsData = dm + "已确定开工日期为:" + status.getContent() + ",需要建档案。该店督导为：" + ddName + "电话为:" + ddTel;
                                logger.info("开工日期已确认=========");
                                JSONObject rsTempObj = weChatSendTempMsgService.setTempParam(dm, null, "建档案", "建档案", DateUtil.getNow(), rsData);
                                //推送消息
//                                weChatSendTempMsgService.sendmsg(rsOpenids, BusConst.MSURL, rsTempObj);

                                pushList.add(new WxPushParamObj(rsOpenids,BusConst.MSURL,rsTempObj));

                                logger.info("=======推送消息给人事部==========");
                            }

                            //将门店同步到brand表
                            taskService.addBrand(status.getProId());

                            //预估完工日期
                        }
//                        else if (status.getTaskId() == 33){
//
//                            TaskStatus status1 = new TaskStatus();
//                            //预估开业时间
//                            status1.setTaskId(22);
//                            //预估完工日期+7天
//                            status1.setContent(DateUtil.addDate(status.getContent(), 7));
//                            status1.setProId(status.getProId());
//                            status1.setStatus(1);
//                            status1.setTaskName("预估开业时间");
//
//                            taskStatuses.add(status1);
//
//                            //流程明细表
//                            TaskFlow taskFlow1 = new TaskFlow();
//                            taskFlow1.setProId(status.getProId());
//                            taskFlow1.setTaskId(22);
//                            taskFlow1.setStatus(1);//1提交；0审核未通过
//                            taskFlow1.setTaskName("预估开业时间");
//                            taskFlow1.setCreateUser(status.getCreateId());
//                            taskFlow1.setCreateTime(new Date());
//
//                            taskFlows.add(taskFlow1);
//
//                        }


                        //修改之前填写信息
                    } else {

                        Long userId = 1311121596351115265L;

                        //修改预估完工日期（只有李伟可以修改）
                        if (status.getTaskId() == 33 && status.getCreateId().equals(userId)) {

                            if (ToolUtil.isNotEmpty(status.getContent()) && ToolUtil.isNotEmpty(status.getNote())){

                                TaskFlow taskFlow = new TaskFlow();
                                taskFlow.setProId(status.getProId());
                                taskFlow.setTaskId(33);
                                taskFlow.setStatus(1);//1提交；0审核未通过
                                taskFlow.setTaskName("预估完工日期");
                                taskFlow.setCreateUser(status.getCreateId());
                                taskFlow.setCreateTime(new Date());

                                //修改status表数据
                                count = taskMapper.updateCon(status);
                                result.append(count > 0 ? 1 : 0);

                                //新增flow表数据
                                count = taskFlowMapper.add(taskFlow);
                                result.append(count > 0 ? 1 : 0);

                            }

                        }

                    }

                }
            }

            if (taskStatuses.size() != 0) {
                //新增信息
                count = taskMapper.addRentList(taskStatuses);
                result.append(count > 0 ? 1 : 0);
            }

            if (taskFlows.size() != 0) {
                //新增流程明细表
                count = taskFlowMapper.addList(taskFlows);
                result.append(count > 0 ? 1 : 0);
            }

            if (gcgMsgs.size() != 0) {
                //修改消息表
                count = this.baseMapper.updateList(gcgMsgs);
//                result.append(count > 0 ? 1 : 0);
            }

        }
        if (result.indexOf("0") >= 0) {
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        } else {

            if (pushList.size()>0){
                for (WxPushParamObj o:pushList){
                    weChatSendTempMsgService.sendmsg(o.getOpenids(), o.getJumpUrl(), o.getTempObj());
                    logger.info("=======推送消息成功==========");
                }

            }

            return 1;
        }
    }

    /**
     * 全部店已完成任务列表(工程)
     *
     * @param userId
     * @return
     */
    @Override
    public List<Map> GCFinish(Long userId, String proName) {

        //查看全部店已完成工程任务
        return this.baseMapper.gcFinish(proName);

    }

    /**
     * 全部店未完成任务列表（工程）
     * @param userId
     * @return
     */
    @Override
    public List GCNoFinish(Long userId, String proName) {


        List list = new ArrayList<>();

        //查看全部店未完成任务(营运总监已通过)
//        page = this.baseMapper.gcNoFinish(pageContext);
        list = this.baseMapper.gcNoFinish(proName);


        return list;
    }

    /**
     * 本店未完成任务列表（工程）
     * @param proId
     * @param userId
     * @return
     */
    @Override
    public Map noFinishGc(int proId, Long userId) {

        //本店未完成任务列表（工程）
       List<Map> list = this.baseMapper.noFinishGc(proId);
       Map mapList = new HashMap<>();

        if (ToolUtil.isNotEmpty(list)){
                for (Map map : list) {
                    //现场踏勘
                    if (map.get("id").equals(30)){

                        mapList.put("taskId", 30);

                        break;

                        //出图日期
                    }else if (map.get("id").equals(31)){

                        mapList.put("taskId", 31);
                        break;

                        //开工日期
                    }else if (map.get("id").equals(32)){

                        mapList.put("taskId", 32);
                        break;

                        //完工日期
                    }else if (map.get("id").equals(34)){

                        mapList.put("taskId", 34);
                        break;

                    }
                }
            }
        return mapList;
    }

    /**
     * 本店已完成任务列表（工程）
     * @param proId
     * @return
     */
    @Override
    public List finishGc(int proId, Integer taskId, Long userId) {

        List<Map> list = new ArrayList<>();

        if (ToolUtil.isNotEmpty(taskId)){
            //李伟完工日期页面
            if (taskId == 34 && userId.equals(1311121596351115265L)){
                //完工日期任务列表（工程）
                list = this.baseMapper.wgList(proId);
            }

        }else {
            //本店已完成任务列表（工程）
            list = this.baseMapper.finishGc(proId);
        }


        return list;
    }

    /**
     * 查看当前任务信息(工程)
     * @param proId
     * @param taskId
     * @param userId
     * @return
     */
    @Override
    public Map GCList(int proId, int taskId, Long userId) {

        Map<String, Object> mapList = new HashMap<>();
        String roleIds = null;

        String phone = userMapper.getPhone(userId);
        if (phone != null && !"".equals(phone)) {
            //根据phone查询当前登陆者角色
            Map map = btyWxMapper.getRole(phone);
            if (map != null && map.size() != 0) {
                roleIds = map.get("roleId").toString();//获取当前登录者角色
            }
        }

        int sta = 0;
        //工程部李伟
        if (userId == 1311121596351115265L){
            sta = 1;
        }
        //工程设计
        else if (ToolUtil.isNotEmpty(roleIds) && roleIds.contains("1373822382028705794")) {
            sta = 2;
        }

        //查看当前任务信息(工程)
        List<TaskStatus> list = this.baseMapper.gCTaskList(proId, taskId);

        mapList.put("list", list);
        mapList.put("sta",sta);

        return mapList;
    }

    /**
     * 已指定营运经理店铺
     * @return
     */
    @Override
    public List yYList(String proName) {
        List<Map> list = this.baseMapper.yYList(proName);
        return list;
    }

    /**
     * 未指定营运经理店铺
     * @return
     */
    @Override
    public List yYNoList(String proName) {
        //未指定营运经理店铺
        List<Map> list = this.baseMapper.yYNoList(proName);
        return list;
    }

    /**
     * 已指定督导店铺
     * @param proName
     * @return
     */
    @Override
    public List dDList(String proName, Long userId) {
        List<Map> list = this.baseMapper.dDList(proName, userId);
        return list;
    }

    /**
     * 未指定督导店铺
     * @param proName
     * @return
     */
    @Override
    public List dDNoList(String proName, Long userId) {
        List<Map> list = this.baseMapper.dDNoList(proName, userId);
        return list;
    }

    /**
     * 本店营运经理
     * @param proId
     * @return
     */
    @Override
    public List yyShop(Integer proId) {
        List<Map> list = this.baseMapper.yyShop(proId);
        return list;
    }

    /**
     * 本店督导
     * @param proId
     * @return
     */
    @Override
    public List ddShop(Integer proId) {
        List<Map> list = this.baseMapper.ddShop(proId);
        return list;
    }

    /**
     * 判断本店任务进度状态
     * @param proId
     * @return
     */
    @Override
    public Map dmStatus(Integer proId) {
        //本店任务进度
        List<Map> list = this.baseMapper.dmStatus(proId);
        Map mapList = new HashMap<>();

        if (ToolUtil.isNotEmpty(list)){
            for (Map map : list) {
                //工程条件，商务条件
                if (map.get("id").equals(2) || map.get("id").equals(3)){

                    mapList.put("sta", 1);

                    break;

                    //工程总监、开发总监确认
                }else if (map.get("id").equals(5) || map.get("id").equals(6)){

                    mapList.put("sta", 2);
                    break;

                    //营运总监确认(确认开店)
                }else if (map.get("id").equals(7)){

                    mapList.put("sta", 3);
                    break;

                    //营运总监指定营运经理
                }else if (map.get("id").equals(40)){

                    mapList.put("sta", 4);
                    break;

                }
            }
        }
        return mapList;
    }

    /**
     * 判断当前任务登录人角色
     * @param proId
     * @return
     */
    @Override
    public Map roleStatus(Integer proId, Long userId) {

        Map<String, Object> mapList = new HashMap<>();
        String roleIds = null;

        String phone = userMapper.getPhone(userId);
        if (phone != null && !"".equals(phone)) {
            //根据phone查询当前登陆者角色
            Map map = btyWxMapper.getRole(phone);
            if (map != null && map.size() != 0) {
                roleIds = map.get("roleId").toString();//获取当前登录者角色
            }
        }

        int status = 0;//0不是该店督导，1是督导
        //查询该店督导电话
        String ddTel = this.baseMapper.getDdTel(proId);

        if(roleIds != null){
            //既是营运经理又是督导
            if (roleIds.contains("1308249608428101633") && roleIds.contains("1283292980305018882")) {
                //是该店督导
                if (ddTel.equals(phone)) {
                    status = 1;
                } else {
                    status = 0;
                }
                //是营运经理不是督导
            } else if (roleIds.contains("1308249608428101633") && !roleIds.contains("1283292980305018882")) {
                status = 0;
                //是督导不是营运经理
            } else if (roleIds.contains("1283292980305018882") && !roleIds.contains("1308249608428101633")) {
                status = 1;
            }
        }

        mapList.put("status", status);
        return mapList;
    }

    /**
     * 修改现场踏勘推迟日期
     * @param proId
     * @param xctk
     * @return
     */
    @Override
    public ResponseData editXctk(Integer proId, String xctk) {
        projectMapper.editXctk(proId, xctk);
        return ResponseData.success();
    }

    /**
     * 查询现场踏勘推迟日期
     * @param proId
     * @return
     */
    @Override
    public Map getXctk(Integer proId) {
        Map map = new HashMap<>();
        Project project = projectMapper.getXctk(proId);
        map.put("project", project);
        return map;
    }

    /**
     * 确认到货状态
     * @param proId
     * @return
     */
    @Override
    public Map getDh(Integer proId) {
        Map map = new HashMap<>();
        ProjectResult project = projectMapper.getDh(proId);
        map.put("project", project);
        return map;
    }

    /**
     * 绑定用户
     * @param name
     * @param phone
     * @return
     */
    @Override
    public Map bindUsers(String name, String phone, String openid, Long deptId) throws ServletException, IOException {

        Map map = new HashMap<>();

        //查询该用户是否存在
        int con = empMapper.getUser(name, phone);
        if (con == 0){
            map.put("sta","该用户不存在");
        }else {
            //获取用户信息
//            Map codeMap = loginService.get(code);
//            if (codeMap != null && codeMap.get("openid") != null){
//                String openid = codeMap.get("openid").toString();
            if (ToolUtil.isNotEmpty(openid)){


                //获取accessToken
                Dict dict = dictService.getOne(new QueryWrapper<Dict>().eq("code", "access_token"));
                String accessToken = "";
                if (ToolUtil.isNotEmpty(dict)) {
                    accessToken = dict.getName();
                }

                //获取用户登录基本信息
                RestTemplate restTemplate = new RestTemplate();
                String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openid + "&lang=zh_CN";
                String userInfo = restTemplate.getForObject(url, String.class);
                JSONObject jsonObject = JSONObject.parseObject(userInfo);
                logger.info("jsonObject====="+jsonObject);

                String unionid = "";
                String nickname = "";
                if(ToolUtil.isNotEmpty(jsonObject)){
                    unionid = jsonObject.get("unionid").toString();
                    nickname = jsonObject.get("nickname").toString();
                    logger.info("unionid+++++++"+unionid);
                }

                int num = 0;
                //查询微信用户是否存在
                int wxCon = btyWxMapper.getCon(openid);
                if (wxCon == 0){

                    BtyWx btyWx = new BtyWx();
                    btyWx.setOpenid(openid);
                    btyWx.setName(name);
                    btyWx.setPhone(phone);
                    btyWx.setDeptId(deptId);
                    btyWx.setUnionid(unionid);
                    btyWx.setNickname(nickname);
                    num = btyWxMapper.insert(btyWx);
                }else {
                    List<BtyWx> wxLists = btyWxMapper.selectList(new QueryWrapper<BtyWx>().eq("openid", openid).eq("phone", phone)
                            .eq("name", name).eq("dept_id", deptId).isNotNull("unionid"));
                    if (wxLists.size() > 0){
                        map.put("sta", "已绑定成功,无须重复绑定!");

                    }else {
                        BtyWx btyWx = btyWxMapper.selectOne(new QueryWrapper<BtyWx>().eq("openid", openid));
                        btyWx.setPhone(phone);
                        btyWx.setName(name);
                        btyWx.setDeptId(deptId);
                        btyWx.setUnionid(unionid);
                        btyWx.setNickname(nickname);
                        num = btyWxMapper.updateById(btyWx);

                    }
                }
                if (num > 0){
                    map.put("sta", "绑定成功！");
                }
            }

        }
        return map;
    }

    /**
     * 审批记录
     * @param account
     * @return
     */
    @Override
    public ResponseData approveList(String account) {
        List<Map> objects = new ArrayList<>();

        if (ToolUtil.isNotEmpty(account)) {


            //股份
            BtyGufenzhuanrangParam param = new BtyGufenzhuanrangParam();

            List<String> list = null;
            //获取用户角色名称
            List<String> roleNames = btyGufenzhuanrangService.roleList(account);
            param.setUserTel(account);

            if (roleNames.contains("总部股份审核")) {
                param.setJiedian("4");

            }else if (roleNames.contains("营运经理")) {
                //查询营运经理所有的店
                list = teamMapper.getYyCode(account);
                if (list.size() > 0) {
                    param.setJiedian("3");
                }else {
                    param.setJiedian("1");
                }

            } else if (roleNames.contains("督导")) {
                //查询督导所有的店
                list = teamMapper.getDdCode(account);

                if (list.size() > 0) {
                    param.setJiedian("2");
                }else {
                    param.setJiedian("1");
                }

            } else if (roleNames.contains("财务股份审核")) {
                param.setJiedian("5");

            } else {
                param.setJiedian("1");//受让方列表
            }

            List<Map> gufenList = btyGufenzhuanrangMapper.gufenzrList(param, list);



            //银行卡
            List<Map> yhkList = new ArrayList<>();
            BtyYhkbgParam btyYhkbgParam = new BtyYhkbgParam();
            if (roleNames.contains("工资卡变更") && roleNames.contains("分红卡变更")){
                yhkList = btyYhkbgMapper.yhkbgList(btyYhkbgParam);

            }else if (roleNames.contains("工资卡变更")){

                btyYhkbgParam.setYhkyt(1);
                yhkList = btyYhkbgMapper.yhkbgList(btyYhkbgParam);

            }else if (roleNames.contains("分红卡变更")){

                btyYhkbgParam.setYhkyt(2);
                yhkList = btyYhkbgMapper.yhkbgList(btyYhkbgParam);

            }





            //工资审核
            List<Map> gzList = new ArrayList();
            //获取用户角色
            User user = userMapper.getByAccount(account);
            Team role = teamMapper.getName(account);

            String username = null;
            List<String> roleNameList = new ArrayList<>();

            if (ToolUtil.isNotEmpty(user)){
                username = user.getName();//用户名字
                String roleId = user.getRoleId();//用户角色id
                List rolds = Arrays.asList(roleId.split(","));
                //获取用户角色名称
                roleNameList = userMapper.getRole(rolds);
            }
            else if (ToolUtil.isNotEmpty(role)){
                username = role.getName();
            }

            if (ToolUtil.isNotEmpty(user) || ToolUtil.isNotEmpty(role)){

                //获取当前月的上个月
                String lastMonth = DateUtil.getLastMonth();
                String roleName = "人事";
                String flag = "";
                String status = "0";

                if(roleNameList.contains(roleName)){
                    gzList = this.btyWageService.listGz(null,null,null, lastMonth, status,null,null, flag);

                }

                if(!("admin".equals(account)) && !roleNames.contains("营运总监") && ToolUtil.isNotEmpty(roleNames) ||
                        (ToolUtil.isNotEmpty(role) && !role.getRankName().equals("营运总监"))){
                    flag="8";
                    //根据姓名查询是否是督导
                    if(!roleNameList.contains("督导")){
                        gzList = this.btyWageService.listGz(null,null,null,lastMonth,status,username,null,flag);
//                        if(gzList.size() == 0){
//                            gzList = this.btyWageService.listGz(null,null,account,lastMonth,status,null,null,flag);
//                        }

                    }else{
                        gzList = this.btyWageService.listGz(null,null,null,lastMonth,status,null,username,flag);
                        if(gzList.size() == 0){
                            gzList = this.btyWageService.listGz(null,null,account,lastMonth,status,null,username,flag);
                        }

                    }
                }else{
                    gzList = this.btyWageService.listGz(null,null,null,lastMonth,status,null,null,flag);

                }
            }


            for (Map gufen : gufenList) {
                gufen.put("type","股份");
                objects.add(gufen);
            }

            for (Map yhkbg : yhkList) {
                yhkbg.put("type","银行卡");
                objects.add(yhkbg);
            }

            for (Map gz : gzList) {
                gz.put("type","工资审核");
                objects.add(gz);
            }

            Collections.sort(objects, new Comparator<Map>() {
                @Override
                public int compare(Map map1, Map map2) {
                    if (ToolUtil.isNotEmpty(map1.get("updateTime")) && ToolUtil.isNotEmpty(map2.get("updateTime"))){
//                        return ((Date) map1.get("updateTime")).compareTo((Date) map2.get("updateTime"));
                        return ((Date) map2.get("updateTime")).compareTo((Date) map1.get("updateTime"));
                    }
                    return 0;
                }
            });

            return ResponseData.success(objects);

        }

        return ResponseData.success(objects);
    }

    /**
     * 本店任务列表
     * @param proId
     * @return
     */
    @Override
    public ResponseData proIdList(Integer proId) {

        //本店任务列表
        List<Map> list = this.baseMapper.proIdList(proId);
        String proName = this.baseMapper.getProName(proId);
        List<Map> objects = new ArrayList<>();

        //objects存储已完成数据
        for (Map map : list) {

            if (ToolUtil.isEmpty(map.get("proName"))){
                map.put("proName", proName);
            }

            if (((int) map.get("id") == 36 || (int) map.get("id") == 37 || (int) map.get("id") == 38) && map.get("sta").equals("已完成")) {
                objects.add(map);
            }
        }

        Map map = new HashMap<>();
        map.put("name", "网上建店");
        map.put("id", 36);
        map.put("proName", proName);
        map.put("proId", proId);
        map.put("status",0);
        map.put("sta","未完成");


        //如果三个都完成，则完成
        //任意一个未完成，则未完成
        if (objects.size() != 3) {

            list.removeAll(objects);
            list.add(map);
        }

        return ResponseData.success(list);
    }

}
